#include "quanlydaily.h"
#include "ui_quanlydaily.h"
#include "qmessagebox.h"
#include "frthutien.h"
#include "frhoadon_cthd.h"
#include "frdoanhthu.h"
#include "frmathang.h"
#include "frloaidaily.h"
#include"frtimkiemdaily.h"

QuanLyDaiLy::QuanLyDaiLy(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QuanLyDaiLy)
{
    ui->setupUi(this);
    thaotac=new ketnoiDB();
     fLoadComboBox();
     fGetTableView();
    fKey(false);


    maDaiLy = 0;
    connect(ui->btnThem,SIGNAL(clicked()),this,SLOT(fSetText()));
    connect(ui->btnSua,SIGNAL(clicked()),this,SLOT(fSetEnabled()));
    connect(ui->btnLuu,SIGNAL(clicked()),this,SLOT(fInsert()));
    connect(ui->btnXoa,SIGNAL(clicked()),this,SLOT(fDelete()));
    connect(ui->btnThoat,SIGNAL(clicked()),this,SLOT(fThoat()));
    connect(ui->actionThongTin,SIGNAL(triggered()),this,SLOT(actionTT()));
    connect(ui->actionNhapHoaDon,SIGNAL(triggered()),this,SLOT(actionHD()));
    connect(ui->actionThongTinMatHang,SIGNAL(triggered()),this,SLOT(actionMH()));
    connect(ui->actionPhieuThu,SIGNAL(triggered()),this,SLOT(actionPT()));
    connect(ui->actionDoanh_Thu,SIGNAL(triggered()),this,SLOT(actionDT()));
    connect(ui->actionTim,SIGNAL(triggered()),this,SLOT(actionTim()));




}


/*
 * Load dữ liệu lên TableView.
 */
void QuanLyDaiLy::fGetTableView()

  {


    qModel = new QSqlQueryModel();
    qModel->setQuery("select d.MaDaiLy,d.TenDaiLy,l.TenLoaiDaiLy,l.MaLoaiDaiLy,d.NgayTiepNhan,d.DienThoai,d.DiaChi,q.TenQuan,q.MaQuan,d.Email from daily d,loaidaily l,quan q where l.MaLoaiDaiLy=d.MaLoaiDaiLy and  q.MaQuan=d.MaQuan");
    //qModel->setQuery("select * from daily");
    ui->tblDaiLy->setModel(qModel);
    ui->tblDaiLy->hideColumn(8);
    connect(ui->tblDaiLy->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),this,SLOT(select(const QItemSelection &)));
    ui->tblDaiLy->selectRow(0);
    ui->tblDaiLy->hideColumn(0);	//Ẩn cột Mã Đại Lý
    ui->tblDaiLy->hideColumn(3);	//Ẩn cột Mã Loại Đại Lý
    ui->tblDaiLy->hideColumn(8); //Ẩn cột Mã Quận
 }// chay

void QuanLyDaiLy::fLoadComboBox()
{
        ui->cboQuan->setModel(thaotac->truyVanDuLieu("select * from quan"));
        ui->cboQuan->setModelColumn(1);

        ui->cboLoaiDaiLy->setModel(thaotac->truyVanDuLieu("select * from loaidaily"));
        ui->cboLoaiDaiLy->setModelColumn(1);
}

//Hàm hiển thị rows tương ứng lên textBox, comboBox, dateEdit.
void QuanLyDaiLy::select(QItemSelection selected)

{
        QModelIndexList list = selected.indexes();
        ui->txtMaDaiLy->setText(qModel->data(list[0],Qt::DisplayRole).toString());
        ui->txtTenDaiLy->setText(qModel->data(list[1],Qt::DisplayRole).toString());
        ui->cboLoaiDaiLy->setCurrentIndex(qModel->data(list[3],Qt::DisplayRole).toInt()-1);
        ui->txtDienThoai->setText(qModel->data(list[5],Qt::DisplayRole).toString());
        ui->txtDiaChi->setText(qModel->data(list[6],Qt::DisplayRole).toString());
        ui->cboQuan->setCurrentIndex(qModel->data(list[8],Qt::DisplayRole).toInt()-1);
        ui->dateEditNgayTiepNhan->setDate(qModel->data(list[4],Qt::DisplayRole).toDate());
        ui->txtEmail->setText(qModel->data(list[9],Qt::DisplayRole).toString());
        maDaiLy=ui->txtMaDaiLy->text().toInt();



}







//Hàm Lưu Dữ Liệu
void QuanLyDaiLy::fInsert()
{
        if(fCheckCondition())
        {
                if(ui->btnThem->text().startsWith("&H"))
                {
                        QString str = "insert into daily values('','"+ui->txtTenDaiLy->text()+"','"+QString::number(ui->cboLoaiDaiLy->currentIndex())+"'"
                                        ",'"+ui->txtDienThoai->text()+"','"+ui->txtDiaChi->text()+"','"+QString::number(ui->cboQuan->currentIndex())+"'"
                                        ",'"+ui->dateEditNgayTiepNhan->text()+"','"+ui->txtEmail->text()+"',0 )";

                        query = thaotac->duyetDuLieu(str);

                        if(query.numRowsAffected()==1)
                        {
                                QMessageBox::information(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Lưu Thành Công."));
                                fGetTableView();
                                fSetText();
                        }
                        else
                        {
                                QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Lưu Không Thành Công."));
                        }
                }
                else if(ui->btnSua->text().startsWith("&H"))
                {
                        QString str = "update daily set TenDaiLy='"+ui->txtTenDaiLy->text()+"',MaLoaiDaiLy="+QString::number(ui->cboLoaiDaiLy->currentIndex()+1)+""
                                                ",DienThoai='"+ui->txtDienThoai->text()+"',DiaChi='"+ui->txtDiaChi->text()+"',MaQuan="+QString::number(ui->cboQuan->currentIndex()+1)+""
                                                ",NgayTiepNhan ='"+ui->dateEditNgayTiepNhan->text()+"',Email ='"+ui->txtEmail->text()+"'where MaDaiLy="+QString::number(maDaiLy)+"";

                        query = thaotac->duyetDuLieu(str);

                        if(query.numRowsAffected()==1)
                        {
                                QMessageBox::information(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Sửa Thành Công."));
                                fSetEnabled();
                                fGetTableView();

                        }
                        else
                        {
                                QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Sửa Không Thành Công."));
                        }

                }
                fKey(false);
        }
}

/*
 * Hàm setText và setEnabled.
 * setText: cho phép thay đổi text của Control.
 * setEnabled: cho phép Control ẩn hiện.
 */
void QuanLyDaiLy::fSetEnabled()
{
        if(ui->btnSua->text().startsWith("&H"))
        {
                ui->btnSua->setText(QString::fromUtf8("&Sửa"));
                ui->btnThem->setEnabled(true);
                ui->btnXoa->setEnabled(true);
                fKey(false);
                ui->btnLuu->setText(QString::fromUtf8("&Lưu"));
                ui->tblDaiLy->selectRow(maDaiLy-1);
        }
        else
        {
                ui->btnLuu->setText(QString::fromUtf8("Cập &Nhật"));
                ui->btnSua->setText(QString::fromUtf8("&Huỷ"));
                ui->btnThem->setEnabled(false);
                ui->btnXoa->setEnabled(false);
                fKey(true);
        }
}

//Hàm Xoá Dữ Liệu
void QuanLyDaiLy::fDelete()
{
        if(QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Bạn Có Muốn Xoá Không?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
        {
                QString str = "delete from daily where MaDaiLy="+QString::number(maDaiLy)+"";

                        query = thaotac->duyetDuLieu(str);

                        if(query.exec())
                        {
                                QMessageBox::information(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Xoá Thành Công."));
                                fGetTableView();
                        }
                        else
                        {
                                QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Xoá Không Thành Công."));
                        }
        }
}

/*
 * Hàm setText và setEnabled.
 * setText: cho phép thay đổi text của Control.
 * setEnabled: cho phép Control ẩn hiện.
 */
void QuanLyDaiLy::fSetText()
{
    if(ui->btnThem->text().startsWith("&H"))
        {
                ui->btnThem->setText(QString::fromUtf8("&Thêm"));
                ui->btnSua->setEnabled(true);
                ui->btnXoa->setEnabled(true);
                fKey(false);
                ui->btnLuu->setText(QString::fromUtf8("&Lưu"));
                ui->tblDaiLy->selectRow(maDaiLy-1);
        }
        else
        {
                ui->btnThem->setText(QString::fromUtf8("&Huỷ"));
                ui->btnSua->setEnabled(false);
                ui->btnXoa->setEnabled(false);
                ui->txtTenDaiLy->setText("");
                ui->txtDiaChi->setText("");
                ui->txtDienThoai->setText("");
                ui->txtEmail->setText("");
                fKey(true);
                ui->tblDaiLy->setFocus();
        }
}




bool QuanLyDaiLy::fCheckCondition()
    {
            bool flag = true;
            if(ui->txtTenDaiLy->text().isEmpty())
                    {
                            QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Vui lòng nhập Tên Đại Lý."));
                            flag = false;
                            ui->txtTenDaiLy->setFocus();
                    }
            else if(ui->txtDienThoai->text().isEmpty())
                            {
                                    QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Vui lòng nhập Số Điện Thoại."));
                                    flag = false;
                                    ui->txtDienThoai->setFocus();

                            }
            else if(ui->txtDiaChi->text().isEmpty())
                                    {
                                            QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Vui lòng nhập Địa Chỉ."));
                                            flag = false;
                                            ui->txtDiaChi->setFocus();
                                    }
            else if(ui->txtEmail->text().isEmpty())
                                            {
                                                    QMessageBox::warning(this,QString::fromUtf8("Thông Báo!"),QString::fromUtf8("Vui lòng nhập Email."));
                                                    flag = false;
                                                    ui->txtEmail->setFocus();
                                            }
            return flag;
    }
 void QuanLyDaiLy::fThoat()
    {
      if(QMessageBox::information(this,QString::fromUtf8("Thông báo"),QString::fromUtf8("Bạn muốn thoát khỏi chương trình ?"),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
        qApp->quit();

    }



QuanLyDaiLy::~QuanLyDaiLy()
{
    delete ui;
}

void QuanLyDaiLy::fKey(bool key)
{
    ui->txtTenDaiLy->setEnabled(key);
    ui->txtDiaChi->setEnabled(key);
    ui->txtDienThoai->setEnabled(key);
    ui->cboLoaiDaiLy->setEnabled(key);
    ui->dateEditNgayTiepNhan->setEnabled(key);
    ui->cboQuan->setEnabled(key);
    ui->txtEmail->setEnabled(key);
    ui->btnLuu->setEnabled(key);
}

void  QuanLyDaiLy::actionTT()
{

    this->hide();
    frloaidaily *frm = new frloaidaily();
    frm->exec();
   this->show();
    fLoadComboBox();
   fGetTableView();

}

void  QuanLyDaiLy::actionHD()
{
        this->hide();
        frHoaDon_CTHD *frm = new frHoaDon_CTHD();
        frm->exec();
        this->show();

}

void  QuanLyDaiLy::actionMH()
{
        this->hide();
        frMatHang *frm = new frMatHang();
        frm->exec();
        this->show();

}

void  QuanLyDaiLy::actionPT()
{
        this->hide();
        frThuTien *frm = new frThuTien();
        frm->exec();
        this->show();

}

void  QuanLyDaiLy::actionDT()
{
        this->hide();
        frDoanhThu *frm = new frDoanhThu();
        frm->exec();
        this->show();

}
void QuanLyDaiLy::actionTim()
{
    this->hide();
    frtimkiemdaily *frm=new frtimkiemdaily();
    frm->exec();
    this->show();
    fLoadComboBox();
   fGetTableView();
}


